package cn.ranko.site.action;

import cn.ranko.bean.easyui.DataGrid;
import cn.ranko.bean.easyui.DataGridParam;
import cn.ranko.bean.easyui.Messager;
import cn.ranko.site.model.SysPermissions;
import cn.ranko.site.model.SysRoles;
import cn.ranko.site.model.SysUsers;
import cn.ranko.site.service.SysPermissionsService;
import cn.ranko.site.service.SysRolesService;
import cn.ranko.utils.ErrorCode;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by ZJ08 on 2016/7/28.
 *
 * 角色控制器
 */
@Controller
@RequestMapping("/site-roles")
public class SysRolesAction {

    @Autowired
    SysRolesService sysRolesService;
    @Autowired
    SysPermissionsService sysPermissionsService;

    /**
     *
     * 角色列表
     *
     * @return
     */
    @RequiresPermissions("site-roles:view")
    @RequestMapping(value = "index.action",method = RequestMethod.GET)
    public String sysroles() {
        return "site/roles/index";
    }

    /**
     * 角色数据载入
     *
     * @return
     */
    @RequiresPermissions("site-roles:view")
    @ResponseBody
    @RequestMapping(value = "loadList.action",method = RequestMethod.POST)
    public DataGrid loadList(){
        DataGrid dataGrid = new DataGrid();
        List<SysRoles> rows = sysRolesService.getRows();
        dataGrid.setRows(rows);
        return dataGrid;
    }


    @RequiresPermissions("site-roles:view")
    @ResponseBody
    @RequestMapping(value = "loadPermissionsList.action",method = RequestMethod.POST)
    public List<SysPermissions> loadPermissionsList(){
        return sysPermissionsService.loadListAll();
    }


    /**
     * 角色添加、界面显示
     * @return
     */
    @RequiresPermissions("site-roles:add")
    @RequestMapping(value = "showadd.action",method = RequestMethod.GET)
    public String showadd(ModelMap modelMap){
        modelMap.addAttribute("id",0);
        modelMap.addAttribute("roles",null);
        return "site/roles/showedit";
    }

    /**
     * 角色编辑、界面显示
     * @return
     */
    @RequiresPermissions("site-roles:edit")
    @RequestMapping(value = "showedit.action",method = RequestMethod.GET)
    public String showedit(Long id, ModelMap modelMap){
        modelMap.addAttribute("id",id);
        if(id!=0){
            SysRoles roles = sysRolesService.getById(id);
            modelMap.addAttribute("roles",roles);
        }
        return "site/roles/showedit";
    }

    /**
     * 角色保存操作
     * @return
     */
    @RequiresPermissions(value = {"site-roles:edit","site-roles:add"},logical = Logical.OR)
    @ResponseBody
    @RequestMapping(value = "save.action",method = RequestMethod.POST)
    public Messager save(SysRoles roles){
        sysRolesService.saveRoles(roles);
        return new Messager(ErrorCode.UPDATE_SUCCESS);
    }

    /**
     * 角色删除操作
     *
     * @param id
     * @return
     */
    @RequiresPermissions("site-roles:delete")
    @RequestMapping(value = "delete.action")
    public @ResponseBody Messager delete(Long id){
        sysRolesService.deleteByIds(id);
        return new Messager(ErrorCode.UPDATE_SUCCESS);
    }
}
